package net.esj.tree.web;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import net.esj.basic.utils.Pagination;
import net.esj.basic.utils.UUIDCreator;
import net.esj.basic.utils.Validators;
import net.esj.basic.view.BaseAction;
import net.esj.tree.TreeConfigurer;
import net.esj.tree.service.TreeService;
import net.esj.tree.utils.TreeEntity;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.util.Assert;

import com.opensymphony.xwork2.ActionSupport;

import freemarker.template.Template;

@Results( { @Result(name = ActionSupport.SUCCESS ,type="jsonview", location = "/ftl/multi.ftl"),
	@Result(name = "debug" , location = "/ftl/multi.ftl")
})
@ParentPackage("melon-default")
public class TreeViewAction extends BaseAction {
	
	private static final int DEFAULT_PAGE_SIZE  =9;

	private String k;
	
	private int hasChild;
	
	private Serializable id;
	
	private int rp = DEFAULT_PAGE_SIZE;
	
	private int page;
	
	private int root;
	
	private int pagination;
	private int remotePage;
	
	private int cascade;
	
	private int clear;
	
	private int multi;
	
	private String key;
	
	private Pagination pager;
	
	private List list;
	
	private boolean debug;
	
	private int maxCount = 1;
	
	private TreeEntity entity;
	
	@Resource
	private TreeConfigurer treeConfigurer;

	@Override
	public String execute() throws Exception {
		TreeService service = treeConfigurer.getService(k);
		if(Validators.isEmpty(id)){
			id = TreeService.DEFAULT_TREE_ROOT_ID_LONG;
		}
		
		//writeJson(service.getChildren(id, hasChild>0));
		entity = service.getEntity(id);
		return SUCCESS;
	}
	
	public String test(){
		debug = true;
		TreeService service = treeConfigurer.getService(k);
		if(Validators.isEmpty(id)){
			id = TreeService.DEFAULT_TREE_ROOT_ID_LONG;
		}
		
		//list = service.getChildren(id, hasChild>0);
		entity = service.getEntity(id);
		return "debug";
	}
	
	public void load(){
		TreeService service = treeConfigurer.getService(k);
		if(Validators.isEmpty(id)){
			id = TreeService.DEFAULT_TREE_ROOT_ID_LONG;
		}
		List list = service.getChildren(id, hasChild>0);
		writeJson(list);
	}
	
	public void page(){
		TreeService service = treeConfigurer.getService(k);
		Pagination pagination = new Pagination(rp, page, service.getGeneralClass());
		pagination = service.getChildrenPage(id, pagination);
		writeJson(pagination);
	}
	
	public void whole(){
		TreeService service = treeConfigurer.getService(k);
		service.getWholeTree(root>0);
	}

	
	
	public String getK() {
		return k;
	}

	public void setK(String k) {
		this.k = k;
	}

	public int getHasChild() {
		return hasChild;
	}

	public void setHasChild(int hasChild) {
		this.hasChild = hasChild;
	}

	public Serializable getId() {
		return id;
	}

	public void setId(Serializable id) {
		if(id.getClass().isArray()){
			id = (Serializable) ((Object[])id)[0];
		}
		this.id = id;
	}
	
	public void setRp(int rp){
		this.rp = rp;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRp() {
		return rp;
	}

	public int getPage() {
		return page;
	}

	public int getRoot() {
		return root;
	}

	public void setRoot(int root) {
		this.root = root;
	}

	public String getKey(){
		if(Validators.isEmpty(key)){
			key = UUIDCreator.create();
		}
		return key; 
	}

	@Override
	public String getText(String aTextName) {
		Assert.hasText(k);
		AbstractMessageSource messageSource = treeConfigurer.getMessageSource(k);
		String msg = messageSource.getMessage(aTextName, null, getCurrentLocale());
		if(!Validators.isEmpty(msg)){
			return msg;
		}
		return super.getText(aTextName);
	}
	
	public String getText(String aTextName, List<?> args) {
		Assert.hasText(k);
		AbstractMessageSource messageSource = treeConfigurer.getMessageSource(k);
		String msg = messageSource.getMessage(aTextName, args.toArray(), getCurrentLocale());
		if(!Validators.isEmpty(msg)){
			return msg;
		}
		return super.getText(aTextName,args);
    }

	@Override
	public String getText(String aTextName,String[] args) {
		Assert.hasText(k);
		AbstractMessageSource messageSource = treeConfigurer.getMessageSource(k);
		String msg = messageSource.getMessage(aTextName, args, getCurrentLocale());
		if(!Validators.isEmpty(msg)){
			return msg;
		}
		return super.getText(aTextName);
	}

	public int getPagination() {
		return pagination;
	}

	public void setPagination(int pagination) {
		this.pagination = pagination;
	}

	public int getClear() {
		return clear;
	}

	public void setClear(int clear) {
		this.clear = clear;
	}

	public int getMulti() {
		return multi;
	}

	public void setMulti(int multi) {
		this.multi = multi;
	}

	public Pagination getPager() {
		return pager;
	}

	public List getList() {
		return list;
	}

	public boolean isDebug() {
		return debug;
	}

	public int getCascade() {
		return cascade;
	}

	public void setCascade(int cascade) {
		this.cascade = cascade;
	}

	public int getMaxCount() {
		return maxCount;
	}

	public void setMaxCount(int maxCount) {
		this.maxCount = maxCount;
	}

	public TreeEntity getEntity() {
		return entity;
	}

	public int getRemotePage() {
		return remotePage;
	}

	public void setRemotePage(int remotePage) {
		this.remotePage = remotePage;
	}
	
}
